<template>
	<div class="oe-teacher-manage" v-loading="loading">
		<top-tip :title="title" :showBack="false"></top-tip>
		<div class="oe-manage-search">
			<!-- <span class="oe-manage-text">教师名称/工号：</span> -->
			<span class="oe-manage-input">
				<a-input placeholder="请输入教师名称/工号" v-model="key">
					<template #prefix>
						<span class="iconfont icon-sousuo"></span>
					</template>
				</a-input>
				<a-button @click.stop="getDataList()" class="chaxun">查询</a-button>
			</span>
			<span class="oe-manage-btns">
				<!-- <a-button type="primary" @click.stop="getDataList">查询</a-button> -->
				<a-button v-show="isShow" @click="showModal" v-if="operationList.includes('add/edit/del')">从职教云导入</a-button>
				<a-button @click="showAdd('add')" v-if="operationList.includes('add/edit/del')">新增</a-button>
				<a-button @click="importExcelQues" v-if="operationList.includes('add/edit/del')">Excel导入</a-button>
				<a-button @click="delTeaAll" v-if="operationList.includes('add/edit/del')" class="piliang">批量删除</a-button>
				<!-- <a-button @click="resetPwd" v-if="operationList.includes('重置密码')">重置密码</a-button> -->
			</span>
		</div>
		<div class="oe-teacher-contain">
			<a-modal :title="titleText" class="oe-studentAdd" :visible="add" :confirmLoading="confirmLoading" @cancel="addCancel">
				<div class="oe-select">
					<a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
						<a-form-model-item label="教师姓名" prop="DisplayName" ref="DisplayName">
							<a-input v-model="form.DisplayName" placeholder="请输入教师姓名" :disabled="isOnlyInfo" class="oe-tooltip-input" @blur="
                  () => {
                    $refs.DisplayName.onFieldBlur();
                  }
                " />
							<a-tooltip placement="bottom" class="oe-push-top__title" v-if="isOnlyInfo">
								<template slot="title">
									<span class="oe-title-content">该用户已开通其他平台，不允许修改姓名</span>
								</template>
								<i class="iconfont icon-wenti"></i>
							</a-tooltip>
						</a-form-model-item>
						<a-form-model-item label="教师用户名" prop="UserName" ref="UserName">
							<a-input v-model="form.UserName" placeholder="请输入教师用户名" :disabled="isDisabled" class="oe-tooltip-input" @blur="
                  () => {
                    $refs.UserName.onFieldBlur();
                  }
                " />
						</a-form-model-item>
						<a-form-model-item label="教师工号" prop="UserNo" ref="UserNo">
							<a-input v-model="form.UserNo" placeholder="请输入教师工号" :disabled="isOnlyInfo" class="oe-tooltip-input" @blur="
                  () => {
                    $refs.UserNo.onFieldBlur();
                  }
                " />
							<a-tooltip placement="bottom" class="oe-push-top__title" v-if="isOnlyInfo">
								<template slot="title">
									<span class="oe-title-content">该用户已开通其他平台，不允许修改工号</span>
								</template>
								<i class="iconfont icon-wenti"></i>
							</a-tooltip>
						</a-form-model-item>
						<a-form-model-item label="手机号" prop="Mobile" ref="Mobile">
							<a-input v-model="form.Mobile" placeholder="请输入手机号" class="oe-tooltip-input" :disabled="isOnlyMoble" />
							<a-tooltip placement="bottom" class="oe-push-top__title" v-if="isOnlyMoble">
								<template slot="title">
									<span class="oe-title-content">该用户手机号已验证通过，不允许修改</span>
								</template>
								<i class="iconfont icon-wenti"></i>
							</a-tooltip>
						</a-form-model-item>
						<a-form-model-item label="邮箱" prop="Email" ref="Email">
							<a-input-group compact>
								<a-auto-complete v-model="form.Email" :data-source="dataSource" style="width: 220px" placeholder="邮箱" @change="handleChangeEmail" />
							</a-input-group>
						</a-form-model-item>
						<a-form-model-item label="所属院系" prop="SubId">
							<a-select v-model="form.SubId" showSearch @change="Changesub" class="oe-tooltip-input" :allowClear="true" optionFilterProp="children" placeholder="请选择所属院系">
								<a-select-option v-for="item in ESSublist" :value="item.id" :key="item.id">
									{{ item.text }}
								</a-select-option>
							</a-select>
						</a-form-model-item>
					</a-form-model>
					<div class="oe-authlabel">
						<label class="oe-modal-text">所属角色：</label>
					</div>
					<span class="oe-modal-input">
						<a-select mode="multiple" style="width: 100%" showSearch :allowClear="true" v-model="CreateName" optionFilterProp="children" @change="selectTeacher" class="oe-tooltip-input" placeholder="请输入角色姓名查询">
							<a-select-option v-for="item in selection" :index="index" :key="item.Id">{{
                item.RoleName
              }}</a-select-option>
						</a-select>
					</span>
				</div>
				<template slot="footer">
					<a-button @click="addSure" type="primary">确定</a-button>
					<a-button @click.stop="addCancel">取消</a-button>
				</template>
			</a-modal>
			<a-modal title="从职教云导入" class="oe-student" :visible="imported" width="70%" @cancel="importedCancel" :afterClose="afterClose">
				<div class="oe-select">
					<span class="oe-question-text">院系：</span>
					<span class="oe-manage-select">
						<a-select v-model="subZjyId" @change="zjysubs" showSearch :allowClear="true" optionFilterProp="children" placeholder="请选择院系" style="width: 20%">
							<a-select-option v-for="(item, index) in zjySublist" :value="item.Id" :index="index" :key="item.Id">
								{{ item.Name }}
							</a-select-option>
						</a-select>
					</span>
					<span class="oe-select-input">
						<a-input placeholder="请输入教师名称/用户名/工号" v-model="keyZjy" />
					</span>
					<a-button type="primary" @click.stop="getZjyDataList">查询</a-button>
				</div>
				<div class="oe-select-table">
					<a-table :rowSelection="{ selectedRowKeys: selectedRowKeysZjy, onChange: onSelectDataZjy }" :columns="importedColumns" :dataSource="importedDatas" :scroll="{ y: 240 }" :pagination="paginationZjy" @change="onZjyTableChange"></a-table>
				</div>
				<template slot="footer">
					<a-button @click="importedSure" type="primary" html-type="submit">确定</a-button>
					<a-button @click.stop="importedCancel">取消</a-button>
				</template>
			</a-modal>
			<a-modal title="excel导入" class="oe-student" :visible="importExcel" width="40%" @cancel="importedCancelExcel">
				<div class="oe-select">
					<span class="oe-manage-select">
						<h2>注意事项：</h2>
						<div>1、导入excel前建议先查看下载模板中的示例，按要求添加导入信息</div>
						<div>2、请按照模板规范顺序导入数据,带"*"为必填项</div>
						<div>3、一次上传的数量请不要大于3000条</div>
					</span>
				</div>
				<div class="oe-select">
					<a href="/exam/excel/导入教师.xlsx">下载模板</a>
				</div>
				<div>
					<div class="clearfix">
						<a-upload :fileList="fileList" :remove="handleRemove" :beforeUpload="beforeUpload" accept=".xlsx">
							<a-button>
								<a-icon type="upload" />选择文件
							</a-button>
						</a-upload>
					</div>
				</div>
				<div class="oe-select-table">
					<a-table :columns="errorColumns" :dataSource="errorList" :pagination="false" v-if="errorList.length > 0"></a-table>
				</div>
				<template slot="footer">
					<a-button type="primary" v-if="!isContinue" @click="handleUpload(0)" :disabled="fileCount === 0" :loading="uploading" style="margin-top: 16px">{{ uploading ? '上传中' : '上传' }}</a-button>
					<a-button type="primary" v-if="isContinue" @click="handleUpload(1)" :disabled="fileCount === 0" :loading="uploading" style="margin-top: 16px">{{ uploading ? '上传中' : '继续上传' }}</a-button>
					<a-button @click.stop="importedCancelExcel">取消</a-button>
				</template>
			</a-modal>
			<a-modal title="教师权限" class="oe-student" :visible="teaPower" width="70%" @cancel="teaPower = false">
				<div class="carte-management-list clearfix">
					<div class="carte-three fl">
						<a-tree v-if="teaPower" :showLine="true" :treeData="treeData" :loadData="onLoadData" @select="onSelect" :defaultSelectedKeys="[ztreeId]"></a-tree>
					</div>
					<div class="carte-list-table fr" style="width: 80%">
						<div class="oe-list-search">
							<span class="oe-list-text">菜单名称：</span>
							<span class="oe-list-input">
								<a-input placeholder="输入菜单名称" v-model="menuKey" class="oe-list-input-text" />
							</span>
							<span class="oe-list-text">操作名称：</span>
							<span class="oe-list-input">
								<a-input placeholder="输入操作名称" v-model="operationKey" class="oe-list-input-text" />
							</span>
							<span class="oe-list-btns">
								<a-button type="primary" @click.stop="getOperation">查询</a-button>
							</span>
						</div>
						<a-table :columns="columnsOpen" :dataSource="data" :pagination="paginationOper" class="oe-list-table">
							<template slot="operation" slot-scope="text, record">
								<span>
									<!-- <a-checkbox-group :options="plainOptions" v-model="record.operation" @change="onOperation(record)" /> -->
									<a-checkbox v-model="record.IsWrite" :disabled="true">可管理</a-checkbox>
									<a-checkbox v-model="record.IsRead" :disabled="true">仅查看</a-checkbox>
								</span>
							</template>
						</a-table>
					</div>
				</div>
				<template slot="footer">
					<a-button @click.stop="teaPower = false">取消</a-button>
				</template>
			</a-modal>
			<div class="oe-manage-content">
				<a-table :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectData }" :columns="columns" :dataSource="datas" :pagination="pagination" @change="onTableChange">
					<span slot="action" slot-scope="text, record">
						<a-tooltip placement="bottom" title="查看权限" v-if="operationList.includes('add/edit/del')">
							<span @click="lookPower(record)" class="action-icon">
								<i class="iconfont icon-chakan"></i>
								<span>查看</span>
							</span>
						</a-tooltip>
						<a-tooltip placement="bottom" title="编辑" v-if="operationList.includes('add/edit/del')" class="bianji">
							<span @click="showAdd(record)" class="action-icon">
								<i class="iconfont icon-edit"></i>
								<span>编辑</span>
							</span>
						</a-tooltip>
						<a-tooltip placement="bottom" title="删除" v-if="operationList.includes('add/edit/del')">
							<span @click="delTea(record)" class="action-icon">
								<i class="iconfont icon-delete"></i>
								<span>删除</span>
							</span>
						</a-tooltip>
					</span>
				</a-table>
			</div>
		</div>
	</div>
</template>

<script>
import topTip from '@/components/top-tip/index.vue';
import GetOperationList from '@/lib/mixin/methods.js';
export default {
	mixins: [GetOperationList],
	components: {
		topTip,
	},
	data() {
		return {
			CreateName: [],
			selection: [],
			titleText: '',
			title: [{ text: '教师管理' }],
			labelCol: { span: 6 },
			wrapperCol: { span: 12 },
			selectedRowKeys: [],
			selectedRowKeysZjy: [],
			mainTeacherIds: [],
			key: '',
			datas: [],
			dataSource: [],
			columns: [
				{
					title: '序号',
					dataIndex: 'Number',
					align: 'center',
				},
				{
					title: '教师名称',
					dataIndex: 'DisPlayName',
					sorter: true,
					align: 'center',
				},
				{
					title: '教师用户名',
					dataIndex: 'UserName',
					sorter: true,
					align: 'center',
				},
				{
					title: '教师工号',
					dataIndex: 'EmployeeNum',
					sorter: true,
					align: 'center',
				},
				{
					title: '所属院系',
					dataIndex: 'SubName',
					sorter: true,
					align: 'center',
				},
				{
					title: '手机号',
					dataIndex: 'Mobile',
					sorter: true,
					align: 'center',
				},
				{
					title: '邮箱',
					dataIndex: 'Email',
					sorter: true,
					align: 'Email',
				},
				{
					title: '所属角色',
					dataIndex: 'RoleNames',
					sorter: true,
					align: 'center',
					ellipsis: true,
				},
				{
					title: '操作',
					key: 'action',
					scopedSlots: { customRender: 'action' },
					align: 'center',
					width: '25%',
				},
			],
			subZjyId: undefined,
			isDisabled: false,
			isOnlyMoble: false,
			isOnlyInfo: false,
			zjysubid: '',
			zjysublist: [],
			ESSublist: [],
			//是否展示从职教云导入
			isShow: false,
			//导入
			imported: false,
			lookItem: [],
			selectType: [],
			loading: false,
			uploading: false,
			importedColumns: [
				{
					title: '序号',
					dataIndex: 'Number',
					align: 'center',
				},
				{
					title: '教师名称',
					dataIndex: 'Name',
					sorter: true,
					align: 'center',
				},
				{
					title: '教师用户名',
					dataIndex: 'UserName',
					sorter: true,
					align: 'center',
				},
				{
					title: '教师工号',
					dataIndex: 'EmployeeNum',
					sorter: true,
					align: 'center',
				},
				{
					title: '所属院系',
					dataIndex: 'SubName',
					sorter: true,
					align: 'center',
				},
			],
			importedDatas: [],
			keyZjy: '',
			// 分页配置
			pagination: {
				pageSizeOptions: ['10', '20', '50', '100', '1000'],
				showSizeChanger: true,
				showTotal(total) {
					return `共 ${total} 条`;
				},
				onShowSizeChange: (current, pageSize) => {
					this.pagination.current = current;
					this.pagination.pageSize = pageSize;
				},
				onChange: (current, pageSize) => {
					this.pagination.current = current;
					this.pagination.pageSize = pageSize;
				},
				buildOptionText(props) {
					return props.value + '条/页';
				},
				current: 1,
				pageSize: 10,
				total: 0,
			},
			//新增
			add: false,
			form: {
				DisplayName: '',
				UserName: '',
				UserNo: '',
				Mobile: '',
				SubId: undefined,
				Email: '',
			},
			paginationZjy: {
				pageSizeOptions: ['10', '20', '50', '100', '1000'],
				showQuickJumper: false,
				showSizeChanger: true,
				showTotal(total) {
					return `共 ${total} 条`;
				},
				onShowSizeChange: (current, pageSize) => {
					this.paginationZjy.current = current;
					this.paginationZjy.pageSize = pageSize;
				},
				onChange: (current, pageSize) => {
					this.paginationZjy.current = current;
					this.paginationZjy.pageSize = pageSize;
				},
				buildOptionText(props) {
					return props.value + '条/页';
				},
				current: 1,
				pageSize: 10,
				total: 0,
			},
			operationList: [],
			IsOpList: [],
			zjySublist: [],
			rules: {
				DisplayName: [
					{ required: true, message: '请输入教师姓名', trigger: 'blur' },
					{ min: 2, max: 30, message: '请输入6-30个字符', trigger: 'blur' },
					{ pattern: /^[^\s]*$/, message: '禁止输入空格', trigger: 'blur' },
				],
				UserName: [
					{ required: true, message: '请输入教师用户名', trigger: 'blur' },
					{ min: 3, max: 30, message: '请输入6-30个字符', trigger: 'blur' },
					{ pattern: /^[^\s]*$/, message: '禁止输入空格', trigger: 'blur' },
				],
				UserNo: [
					{ required: true, message: '请输入教师工号', trigger: 'blur' },
					{ min: 3, max: 30, message: '请输入6-30个字符', trigger: 'blur' },
					{ pattern: /^[^\s]*$/, message: '禁止输入空格', trigger: 'blur' },
				],
				SubId: [{ required: true, message: '请选择院系', trigger: 'change' }],
			},
			lock: false,
			importExcel: false,
			fileList: [],
			fileCount: 0,
			errorList: [],
			errorColumns: [
				{
					title: '序号',
					dataIndex: 'Number',
					align: 'center',
				},
				{
					title: '明细',
					dataIndex: 'Title',
					align: 'center',
				},
			],
			isContinue: false,
			operationKey: '',
			menuKey: '',
			ztreeId: '',
			childList: [],
			cahceZtreeId: '',
			data: [],
			nowUserId: '',
			teaPower: false,
			columnsOpen: [
				{ title: '序号', dataIndex: 'key' },
				{ title: '菜单名称 ', dataIndex: 'MenuName' },
				{ title: '操作名称 ', dataIndex: 'Name' },
				{ title: '所属角色 ', dataIndex: 'RoleNames' },
				// { title: '所属路径', dataIndex: 'PageUrl' },
				{ title: '操作', dataIndex: 'operation', width: '200px', scopedSlots: { customRender: 'operation' } },
			],
			paginationOper: {
				pageSizeOptions: ['10', '20', '50', '100', '1000'],
				showQuickJumper: true,
				showSizeChanger: true,
				showTotal(total) {
					return `共 ${total} 条`;
				},
				onShowSizeChange: (current, pageSize) => {
					this.paginationOper.current = current;
					this.paginationOper.pageSize = pageSize;
					this.getOperation();
				},
				onChange: (current, pageSize) => {
					this.paginationOper.current = current;
					this.paginationOper.pageSize = pageSize;
					this.getOperation();
				},
				buildOptionText(props) {
					return props.value + '条/页';
				},
				current: 1,
				pageSize: 10,
			},
			treeData: [],
			rowList: [],
		};
	},
	mounted() {
		this.getOpeartion();
	},
	methods: {
		// //搜索多选
		selectTeacher(val) {
			this.mainTeacherIds = val;
		},
		//获取角色列表
		getRoleList() {
			this.$http
				.post('/api/SchoolManager/teacher/getRoleList', {
					key: this.selected,
				})
				.then((res) => {
					this.selection = res.list;
				});
		},
		// 点击table排序
		onTableChange(pagenation, filter, sort) {
			let sortOrder = '';
			if (sort.order) {
				if (sort.order == 'ascend') {
					sortOrder = sort.columnKey;
				} else if (sort.order == 'descend') {
					sortOrder = '-' + sort.columnKey;
				} else {
					sortOrder = '';
				}
			}
			this.getDataList(pagenation.current, pagenation.pageSize, sortOrder);
		},
		getIsWrite(type) {
			type = 'add/edit/del';
			let index = this.IsOpList.findIndex((el) => {
				return el.Name === type;
			});
			if (index > -1) {
				if (this.IsOpList[index].IsRead && !this.IsOpList[index].IsWrite) {
					this.$message.info('您没有该操作权限！');
					return false;
				} else {
					return true;
				}
			}
		},
		handleChangeEmail(value) {
			this.dataSource =
				!value || value.indexOf('@') >= 0 ? [] : [`${value}@gmail.com`, `${value}@163.com`, `${value}@qq.com`];
		},
		// 获取页面操作
		getOpeartion() {
			this.loading = true;
			this.GetOperationList().then((rst) => {
				this.loading = false;
				this.operationList = [];
				this.IsOpList = [];
				if (rst.code === 1) {
					if (rst.list.length) {
						rst.list.forEach((item) => {
							if (item.IsWrite) {
								this.operationList.push(item.Name);
							}
							this.IsOpList = rst.list;
						});
					}
					this.getDataList();
					this.getESSubList();
					this.getZJYSubList();
					this.getRoleList();
				} else {
					this.$message.error(rst.msg);
				}
			});
		},
		//获取数据列表
		getDataList(page, pageSize, sortOrder) {
			this.loading = true;
			this.$http
				.post('/api/SchoolManager/teacher/getTeacherList', {
					key: this.key,
					sort: sortOrder || '',
					page: page || 1,
					pageSize: pageSize || 10,
				})
				.then((rst) => {
					this.loading = false;
					if (rst.code > 0) {
						if (rst.isZjySchool) {
							this.isShow = true;
						}
						this.datas = [];
						this.pagination.current = rst.pagination.pageIndex;
						this.pagination.pageSize = rst.pagination.pageSize;
						this.pagination.total = rst.pagination.totalCount;
						if (rst.list.length) {
							rst.list.forEach((item, index) => {
								let number = (rst.pagination.pageIndex - 1) * rst.pagination.pageSize + index + 1;
								this.datas.push({
									...item,
									key: item.Id,
									Number: number,
								});
							});
						}
					} else {
						this.$message.error(rst.msg);
					}
				})
				.catch(() => {
					// this.$message.error('接口错误')
					this.loading = false;
				})
				.finally(() => {
					this.loading = false;
				});
		},
		zjysubs(value) {
			this.subZjyId = value;
		},
		onSelect(selectedKeys, info) {
			this.pagination.current = 1;
			this.pagination.pageSize = 10;
			if (selectedKeys.length) {
				this.cahceZtreeId = selectedKeys[0];
			}
			this.ztreeId = this.cahceZtreeId;
			this.getOperation();
		},
		//查看权限
		lookPower(value) {
			this.treeData = [];
			this.loading = true;
			this.nowUserId = value.Id;
			this.$http
				.post('/api/SchoolManager/rolemenu/getUserPowerZtree', { userId: value.Id })
				.then((res) => {
					if (res.length == 0) {
						this.$message.error('教师暂无权限');
						return false;
					}
					this.treeData = this.getZtreeData(res);
					this.ztreeId = res[0].id;
					this.getOperation();
					this.teaPower = true;
				})
				.catch(() => {
					this.loading = false;
					this.$message.error('接口出错，请稍后重试');
				})
				.finally(() => {
					this.loading = false;
				});
		},
		getOperation() {
			this.loading = true;
			this.$http
				.post('/api/SchoolManager/rolemenu/getPageOperation', {
					menuKey: this.menuKey,
					operationKey: this.operationKey,
					pageUrl: this.ztreeId, //菜单id
					userId: this.nowUserId,
					pageSize: this.paginationOper.pageSize,
					page: this.paginationOper.current,
				})
				.then((res) => {
					this.loading = false;
					if (res.code > 0) {
						this.data = res.list;
						for (let i = 0; i < res.list.length; i++) {
							this.data[i].key = i + 1;
						}
					} else {
						this.$message.error(res.msg);
					}
				}).finally(()=>{
					this.loading = false;
        })
		},
		// 重新构造树状图结构
		getZtreeData(data) {
			var ztreeData = [];
			for (let i = 0; i < data.length; i++) {
				let lis = {
					key: data[i].id,
					title: data[i].name,
					isParent: data[i].isParent,
				};
				ztreeData.push(lis);
			}
			return ztreeData;
		},
		// 树状图异步加载
		onLoadData(treeNode) {
			if (treeNode.dataRef.isParent) {
				return this.getZtreeTwo(treeNode.dataRef.key).then((res) => {
					if (treeNode.dataRef.children) {
						return;
					}
					treeNode.dataRef.children = res;
					this.treeData = [...this.treeData];
					return res;
				});
			} else {
				return Promise.resolve();
			}
		},
		getZtreeTwo(Id) {
			this.childList = [];
			return this.$http
				.post('/api/SchoolManager/rolemenu/getUserPowerZtree', {
					Id: Id,
					userId: this.nowUserId,
				})
				.then(this.getZtreeData);
		},
		//新增
		showAdd(value) {
			if (value == 'add') {
				if (!this.getIsWrite('新增')) {
					return;
				}
				this.form.DisplayName = '';
				this.form.UserName = '';
				this.form.UserNo = '';
				this.Mobile = '';
				this.form.SubId = undefined;
				this.CreateName = [];
				this.titleText = '新增信息';
				this.isDisabled = false;
				this.isOnlyInfo = false;
				this.isOnlyMoble = false;
			} else {
				if (!this.getIsWrite('编辑')) {
					return;
				}
				this.isDisabled = true;
				this.lookItem = value;
				this.titleText = '编辑信息';
				this.$http
					.post('/api/SchoolManager/teacher/editInfo', {
						id: value.Id,
					})
					.then((res) => {
						if (res.code > 0) {
							this.form.DisplayName = res.info.DisplayName;
							this.form.UserName = res.info.UserName;
							this.form.UserNo = res.info.UserNo;
							this.form.Mobile = res.info.Mobile;
							this.form.Email = res.info.Email ? res.info.Email : undefined;
							this.form.SubId = res.info.SubId ? res.info.SubId : undefined;
							this.isOnlyMoble = res.isOnlyMoble;
							this.isOnlyInfo = res.isOnlyInfo;
							this.CreateName = [];
							res.roleList.forEach((item) => {
								let index = this.selection.findIndex((role) => {
									return role.Id === item.Id;
								});
								if (index > -1) {
									this.CreateName.push(this.selection[index].Id);
									this.mainTeacherIds.push(this.selection[index].Id);
								}
							});
						} else {
							this.$message.error(res.msg);
						}
					})
					.finally(() => {
						this.loading = false;
					});
			}
			this.add = true;
		},
		//获取院系
		getESSubList() {
			this.$http
				.post('/api/common/localstorage/getESSubList', {
					q: this.selectType,
				})
				.then((rst) => {
					this.ESSublist = rst.list;
				});
		},
		//获取职教云院系
		getZJYSubList() {
			this.$http
				.post('/api/common/localstorage/getSubList', {
					q: this.selectType,
				})
				.then((rst) => {
					this.zjySublist = rst.list;
				});
		},
		addCancel() {
			this.add = false;
			this.mainTeacherIds = [];
			this.mobile = '';
			this.$refs.ruleForm.resetFields();
		},
		// 保存信息
		addSure() {
			let id = this.titleText === '编辑信息' ? this.lookItem.Id : '';
			this.loading = true;
			if (this.titleText === '编辑信息') {
				if (!this.getIsWrite('编辑')) {
					return;
				}
			} else {
				if (!this.getIsWrite('新增')) {
					return;
				}
			}
			if (this.form.Mobile.length != 0) {
				if (this.form.Mobile.length != 11) {
					this.loading = false;
					this.$message.info('请输入十一位有效手机号');
					return;
				}
			}
			this.$refs.ruleForm.validate((valid) => {
				if (valid) {
					this.$http
						.post('/api/SchoolManager/teacher/saveInfo', {
							id: id,
							displayName: this.form.DisplayName,
							userName: this.form.UserName,
							employeeNum: this.form.UserNo,
							subId: this.form.SubId,
							mobile: this.form.Mobile,
							roleIds: this.mainTeacherIds,
							email: this.form.Email,
						})
						.then((rst) => {
							this.loading = false;
							if (rst.code > 0) {
								this.add = false;
								this.$message.success(rst.msg);
								this.lookItem = '';
								this.CreateName = '';
								this.form.Mobile = '';
								this.mainTeacherIds = [];
								this.getDataList();
							} else {
								this.$message.error(rst.msg);
							}
						});
				} else {
					this.loading = false;
					return false;
				}
			});
		},
		//删除信息
		delTea(value) {
			if (!this.getIsWrite('删除')) {
				return;
			}
			this.$confirm({
				title: '提示',
				content: '确认删除选择的数据吗？',
				centered: true,
				onOk: () => {
					this.$http
						.post('/api/SchoolManager/teacher/deleteTeacher', {
							ids: value.Id, //要删除的成员id数组
						})
						.then((res) => {
							if (res.code > 0) {
								this.$message.success(res.msg);
								this.getDataList();
							} else {
								this.$message.error(res.msg);
							}
						})
						.finally(() => {
							this.loading = false;
						});
				},
				onCancel() {},
			});
		},
		//批量删除信息
		delTeaAll() {
			if (!this.getIsWrite('删除')) {
				return;
			}
			if (this.selectedRowKeys.length === 0) {
				this.$message.error('请选择要删除的数据');
				return;
			}
			this.$confirm({
				title: '提示',
				content: '确认删除选择的数据吗？',
				centered: true,
				onOk: () => {
					this.$http
						.post('/api/SchoolManager/teacher/deleteTeacher', {
							ids: this.selectedRowKeys, //要删除的成员id数组
						})
						.then((res) => {
							if (res.code > 0) {
								this.$message.success(res.msg);
								this.getDataList();
								this.selectedRowKeys = [];
							} else {
								this.$message.error(res.msg);
							}
						});
				},
				onCancel() {},
			});
		},
		//批量重置密码
		resetPwd() {
			if (!this.getIsWrite('重置密码')) {
				return;
			}
			if (this.selectedRowKeys.length === 0) {
				this.$message.error('请选择要重置密码的数据');
				return;
			}
			this.$confirm({
				title: '提示',
				content: '确认重置选择数据的密码吗？',
				centered: true,
				onOk: () => {
					this.$http
						.post('/api/common/accounts/resetPwd', {
							id: this.selectedRowKeys,
						})
						.then((res) => {
							if (res.code > 0) {
								this.$message.success(res.msg);
								this.selectedRowKeys = [];
							} else {
								this.$message.error(res.msg);
							}
						});
				},
				onCancel() {},
			});
		},
		//导入
		showModal() {
			if (!this.getIsWrite('新增')) {
				return;
			}
			this.imported = true;
			this.getZjyDataList();
		},
		// 点击table排序
		onZjyTableChange(paginationZjy, filter, sort) {
			let sortOrder = '';
			if (sort.order) {
				if (sort.order == 'ascend') {
					sortOrder = sort.columnKey;
				} else if (sort.order == 'descend') {
					sortOrder = '-' + sort.columnKey;
				} else {
					sortOrder = '';
				}
			}
			this.getZjyDataList(paginationZjy.current, paginationZjy.pageSize, sortOrder);
		},
		//获取职教云教师列表
		getZjyDataList(page, pageSize, sortOrder) {
			this.loading = true;
			this.$http
				.post('/api/SchoolManager/teacher/getZJYClassList', {
					subId: this.subZjyId,
					key: this.keyZjy,
					sort: sortOrder || '',
					page: page || 1,
					pageSize: pageSize || 10,
				})
				.then((rst) => {
					this.loading = false;
					if (rst.code == 1) {
						this.selectedRowKeysZjy = [];
						this.importedDatas = [];
						this.paginationZjy.current = rst.pagination.pageIndex;
						this.paginationZjy.pageSize = rst.pagination.pageSize;
						this.paginationZjy.total = rst.pagination.totalCount;
						if (rst.list.length) {
							rst.list.forEach((item, index) => {
								let number = (rst.pagination.pageIndex - 1) * rst.pagination.pageSize + index + 1;
								this.importedDatas.push({
									...item,
									key: item.Id,
									Number: number,
								});
							});
						}
					} else {
						this.$message.error(rst.msg);
					}
				});
		},
		importedCancel() {
			this.imported = false;
			this.keyZjy = '';
			this.subZjyId = undefined;
		},
		importedSure() {
			this.saveImpot();
		},
		//保存导入的数据
		saveImpot() {
			if (!this.getIsWrite('新增')) {
				return;
			}
			if (this.selectedRowKeysZjy.length === 0) {
				this.$message.error('请选择要添加的数据');
				return;
			}
			this.$confirm({
				title: '提示',
				content: '确认添加选择的数据吗？',
				centered: true,
				onOk: () => {
					this.$http
						.post('/api/SchoolManager/teacher/importZJYTeacher', {
							ids: this.selectedRowKeysZjy, //要删除的成员id数组
						})
						.then((res) => {
							if (res.code > 0) {
								this.imported = false;
								this.$message.success(res.msg);
								this.getDataList();
								this.getESSubList();
								this.selectedRowKeysZjy = [];
								this.subZjyId = undefined;
							} else {
								this.$message.error(res.msg);
							}
						});
				},
				onCancel() {},
			});
		},
		//多选
		onSelectData(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys;
		},
		//选择要导入的数据
		onSelectDataZjy(selectedRowKeys, selectedRows) {
			this.selectedRowKeysZjy = selectedRowKeys;
		},
		start() {
			this.loading = true;
			setTimeout(() => {
				this.loading = false;
				this.selectedRowKeys = [];
			}, 1000);
		},
		onSelectChange(selectedRowKeys) {
			this.selectedRowKeys = selectedRowKeys;
		},
		importedCancelExcel() {
			this.importExcel = false;
			this.isContinue = false;
			this.rowList = [];
		},
		//点击excel导入
		importExcelQues() {
			if (!this.getIsWrite('新增')) {
				return;
			}
			this.fileCount = 0;
			this.errorList = [];
			this.fileList = [];
			this.importExcel = true;
			this.isContinue = false;
			this.rowList = [];
		},
		//删除文件
		handleRemove(file) {
			const index = this.fileList.indexOf(file);
			const newFileList = this.fileList.slice();
			newFileList.splice(index, 1);
			this.fileList = newFileList;
			this.errorList = [];
			this.isContinue = false;
			this.rowList = [];
		},
		//选择文件
		beforeUpload(file) {
			this.fileList = [];
			this.fileCount = 1;
			this.fileList = [...this.fileList, file];
			this.isContinue = false;
			this.rowList = [];
			return false;
		},
		//上传
		handleUpload(type) {
			if (this.lock) {
				return;
			}
			var api = '/api/schoolManager/teacher/importExcelTea';
			if (type == 1) {
				api = '/api/schoolManager/teacher/continueImpTea';
			} else {
				this.rowList = [];
			}
			const { fileList } = this;
			const formData = new FormData();
			fileList.forEach((file) => {
				formData.append('files[]', file);
				formData.append('rowList', this.rowList);
			});
			this.lock = true;
			this.uploading = true;
			this.$http.post(api, formData).then((res) => {
				this.lock = false;
				this.isContinue = false;
				this.errorList = [];
				this.uploading = false;
				if (res.code > 0) {
					this.fileCount = 0;
					this.importExcel = false;
					this.getDataList();
					this.$message.success(res.msg);
				} else if (res.code == -100) {
					this.isContinue = true;
					this.rowList = res.errorRowList;
					this.$message.error(res.msg);
					if (res.list.length) {
						res.list.forEach((item, index) => {
							this.errorList.push({
								Title: item,
								Number: index + 1,
							});
						});
					}
				} else {
					this.fileCount = 0;
					this.$message.error(res.msg);
				}
			});
		},
		// 关闭导入职教云的回调
		afterClose() {
			this.selectedRowKeysZjy = [];
			this.keyZjy = '';
			this.subZjyId = undefined;
		},
	},
};
</script>

<style></style>
